package cn.com.wxd.servlet.wechat;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.wxd.servlet.wechat.util.SignUtil;
/**
 * Title:WechatServletInterFace.java
 * Description:cn.com.wxd.servlet.wechat
 * 
 * Company: wxd网站系统
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-8-24 下午3:25:42
 *
 * @author WangXuDong
 * @version 1.0
 */
public class WechatServletInterFace extends HttpServlet {  

    private static final long serialVersionUID = 7534232612712558319L;  
    
    public WechatServletInterFace() {  
        super();  
    }  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        try {
        	System.out.println("开始介入");
            // 微信加密签名  
            String signature = request.getParameter("signature");  
            // 时间戳  
            String timestamp = request.getParameter("timestamp");  
            // 随机数  
            String nonce = request.getParameter("nonce");  
            // 随机字符串  
            String echostr = request.getParameter("echostr");
            if(signature==null||timestamp==null||nonce==null||echostr==null){
            	System.out.println("请求非法");
            	return;
            }
            PrintWriter out = response.getWriter();  
            // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败  
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {  
                out.print(echostr);  
            }  
            out.close();  
            out = null;  
  
        } catch (Exception e) {
        	e.printStackTrace();
        }  
    }  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {
    }  
}  
